﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aribaccio;
using Aribaccio.Device;
using Aribaccio.Logger;
using BDPAutodetect.Device;
using BDPAutodetect.Hardware;
using BDPAutodetect.Models;

namespace BDPAutodetect.Mgrcore
{
    /// <summary>
    /// DIDO输入输出板 检测设备通讯
    /// </summary>
    public class DetectDeviceSensorSign : DetectDeviceModbus
    {
        /// <summary>
        /// 检测任务线程
        /// </summary>
        protected PeriodicTaskManual MonitorTask { get; set; }
        /// <summary>
        /// 设开关指标
        /// </summary>
        protected MTargetSlave? TargetSlaveSwitch { get; set; }
        /// <summary>
        /// 开关状态
        /// </summary>
        public SensorSignSwitchStatus SwitchStatus { get; protected set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="argDevice">设备连接对象</param>
        /// <param name="argTargetSlaves">设备指标协议数据集</param>
        public DetectDeviceSensorSign(DeviceSerialModbus<IDevicePara> argDevice, List<MTargetSlave> argTargetSlaves)
            : base($"DIDO输入输出板检测设备通讯", DeviceType.SensorSign, DeviceStationType.None, argDevice, argTargetSlaves)
        {
            this.SwitchStatus = new SensorSignSwitchStatus();
            this.MonitorTask = new PeriodicTaskManual($"{this.Name}实时监视任务", 40, MonitorTaskAction, true, true);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
            this.TargetSlaveSwitch = this.GetTargetSlave(DeviceStationType.None, DeviceTargetType.SensorSign_SwitchRead);
        }

        /// <summary>
        /// 写入设备蜂鸣器开关
        /// </summary>
        /// <param name="argStationType">工位类型</param>
        /// <param name="argSwitchType">开关类型</param>
        /// <returns></returns>
        public bool WriteDeviceHummer(DeviceStationType argStationType, SwitchType argSwitchType)
        {
            return this.WriteDeviceData(argStationType, DeviceTargetType.SensorSign_HummerWrite, argSwitchType);
        }

        /// <summary>
        /// 写入设备数据
        /// </summary>
        /// <param name="argStationType">工位类型</param>
        /// <param name="argTargetLedLight">设备指标类型</param>
        /// <param name="argSwitchType">开关类型</param>
        /// <returns></returns>
        public bool WriteDeviceData(DeviceStationType argStationType, DeviceTargetType argTargetLedLight, SwitchType argSwitchType)
        {
            //定义发送数据
            var switchData = DeviceHelper.Instance.GetSwitchData(argSwitchType);
            StructInputShort structInputShort = new StructInputShort(switchData);
            //写入设备
            return this.WriteDeviceData(argStationType, argTargetLedLight, structInputShort);
        }

        /// <summary>
        /// 读取设备数据
        /// </summary>
        /// <returns></returns>
        public ResultModel<byte> ReadDeviceDataSwitch()
        {
            if (this.TargetSlaveSwitch == null) return new ResultModel<byte>(ResultStatus.Failure);

            StructInputShort structInputShort = new StructInputShort(0x03);
            return this.ReadDeviceDataByte(this.TargetSlaveSwitch, structInputShort);
        }

        /// <summary>
        /// 执行线程任务
        /// </summary>
        protected void MonitorTaskAction()
        {
            if (!this.IsOpen) { Thread.Sleep(100); return; }

            ResultModel<byte> resultModel = this.ReadDeviceDataSwitch();
            //异常返回，或者数据为0(目前只使用3个工位，如果全使用就会有问题)，都表示失败
            if (!resultModel.ResultState || resultModel.Data <= 0) return;

            //解析返回数据字节->数据位bit
            this.SwitchStatus.UpdateState(resultModel.Data);
            if (this.SwitchStatus.StationStatus1 || this.SwitchStatus.StationStatus2 || this.SwitchStatus.StationStatus3)
            {
                //LoggerMgrcore.Instance.WriteInfo($"[{this.Name}] => Switch：{resultModel.Data.ToString("X")}");
            }
        }
    }
}
